package com.smart_campus.sys.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.smart_campus.sys.common.DataGridView;
import com.smart_campus.sys.vo.NoticeVo;
import com.smart_campus.sys.vo.RoleVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.smart_campus.sys.mapper.RoleMapper;
import com.smart_campus.sys.domain.Role;
import com.smart_campus.sys.service.RoleService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @aytgor 陈鹏
 * @date 2020/6/10 14:22
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;


    /**
     * 保存角色和菜单之间的关系
     *
     * @param rid
     * @param mids
     */
    @Override
    public void saveRoleMenu(Integer rid, Integer[] mids) {
        this.roleMapper.deleteRoleMenuByRid(rid);
        if (mids != null && mids.length > 0) {
            for (Integer mid : mids) {
                this.roleMapper.insertRoleMenu(rid, mid);
            }
        }
    }

    /**
     * @param roleVo
     * @return DataGridView
     * 角色分页方法
     */
    @Override
    public DataGridView selectRole(RoleVo roleVo) {
        Page<Object> page = PageHelper.startPage(roleVo.getPage(), roleVo.getLimit());//分页数据格式
        List<RoleVo> roleVos = roleMapper.selectRole(roleVo);//返回的全部数据
//        System.out.println(roleVos.get(0).getCreateTime());
        return new DataGridView(page.getTotal(), roleVos);
    }

    /**
     * 查询角色权限
     *
     * @param id
     * @return List<Integer>
     */
    @Override
    public Integer[] selectMid(Integer id) {
        return roleMapper.selectMid(id);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        roleMapper.DeleteRoleMenu(id);
        return roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Role record) {
        return roleMapper.insert(record);
    }

    @Override
    public int insertSelective(Role record) {
        return roleMapper.insertSelective(record);
    }

    @Override
    public Role selectByPrimaryKey(Integer id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Role record) {
        return roleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Role record) {
        return roleMapper.updateByPrimaryKey(record);
    }


    /**
     * DataGridView
     * @param id
     * @return DataGridView
     */
    @Override
    public DataGridView queryAllAvailableRoleNoPage(Integer id) {
        //查询全部的可用角色
        List<Role> roles = this.roleMapper.selectList(1);
        //根据用户ID查询已拥有的角色ID
        List<Integer> roleIds=this.roleMapper.queryRoleIdsByUserId(id);
        //创建返回list
        List<Map<String,Object>> lists=new ArrayList<>();
        //循环判断用户的角色有那些并赋值为true
        for (Role role : roles) {
            Boolean LAY_CHECKED=false;
            for (Integer roleId : roleIds) {
                if(role.getId().equals(roleId)){
                    LAY_CHECKED=true;
                    break;
                }
            }
            Map<String,Object> map=new HashMap<>();
            map.put("id",role.getId());
            map.put("name",role.getName());
            map.put("remark",role.getRemark());
            map.put("LAY_CHECKED",LAY_CHECKED);
            lists.add(map);
        }
        return new DataGridView(Long.valueOf(lists.size()),lists);
    }

    /**
     * 根据用户id查询角色id的集合
     * @param
     * @return
     */
    @Override
    public List<String> queryRoleNamesByUserId(Role role,Integer userId) {
        List<Integer> roleIds = roleMapper.queryRoleIdsByUserId(userId);
        if(roleIds!=null&&roleIds.size()>0){
            List<Role> rolesObject = this.roleMapper.selectLists(role,roleIds);
            List<String> roles=new ArrayList<>();
            for (Role role1 : rolesObject) {
                roles.add(role1.getName());
            }
            return roles;
        }else {
            return null;
        }

    }

}
